using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Xml;
using System.IO;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;

namespace LocalizationToolkit_DAL
{
    public class Elements:Localization
    {
        
        public DataSet GetElements(int ApplicationID, int DataSourceID){
            try
            {
                DataSet ds= new DataSet();
                               
                this.cmd = new SqlCommand("usp_get_elements_in_application_per_ds");
                this.cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter par1 = new SqlParameter("@ApplicationID", SqlDbType.Int);
                par1.Value = ApplicationID;
                this.cmd.Parameters.Add(par1);
                SqlParameter par2 = new SqlParameter("@DataSourceID", SqlDbType.Int);
                par2.Value = DataSourceID;
                this.cmd.Parameters.Add(par2);

                ds = ExecuteDatabaseCommand(ds);
                
                return ds;
            }
            finally
            {
            }

        }

       
        public DataSet GetElements(int ApplicationID)
        {
            try
            {
                DataSet ds = new DataSet();
                


                this.cmd = new SqlCommand("usp_get_elements_in_application");
                this.cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter par = new SqlParameter("@ApplicationID", SqlDbType.Int);
                par.Value = ApplicationID;
                this.cmd.Parameters.Add(par);

                ds = ExecuteDatabaseCommand(ds);

                return ds;
            }
            finally
            {
            }

        }
        public DataSet GetElementsPerCulture(int ApplicationID, int ApplicationCultureID)
        {
            try
            {
                DataSet ds = new DataSet();
              


                this.cmd = new SqlCommand("usp_get_elements_in_application_per_culture");
                this.cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter par = new SqlParameter("@ApplicationID", SqlDbType.Int);
                par.Value = ApplicationID;
                this.cmd.Parameters.Add(par);

                ds = ExecuteDatabaseCommand(ds);

                return ds;
            }
            finally
            {
            }

        }


        public DataSet GetElementByID(int ElementID,int ApplicationID)
        {
            try
            {
                DataSet ds = new DataSet();
               

                this.cmd = new SqlCommand("usp_get_element_by_ID");
                this.cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter par1 = new SqlParameter("@ElementID", SqlDbType.Int);
                par1.Value = ElementID;
                this.cmd.Parameters.Add(par1);
                SqlParameter par = new SqlParameter("@ApplicationID", SqlDbType.Int);
                par.Value = ApplicationID;
                this.cmd.Parameters.Add(par);

                ds = ExecuteDatabaseCommand(ds);

                return ds;
            }
            finally
            {
            }

        }

        public DataSet GetDataSources(int ApplicationID)
        {
            try
            {
                DataSet ds = new DataSet();
               

                this.cmd = new SqlCommand("dbo.usp_get_datasources_per_app");
                this.cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter par = new SqlParameter("@ApplicationID", SqlDbType.Int);
                par.Value = ApplicationID;
                this.cmd.Parameters.Add(par);
                ds = ExecuteDatabaseCommand(ds);

                return ds;
               
            }
            finally
            {
            }

        }

        public Boolean DeleteElement(int ElementID)
        {
            try
            {
               


                this.cmd = new SqlCommand("dbo.usp_delete_element");
                this.cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter par = new SqlParameter("@ElementID", SqlDbType.Int);
                par.Value = ElementID;
                this.cmd.Parameters.Add(par);

                if (ExecuteNonQuery() > 0)
                    return true;
                else
                    return false;

            }
            finally
            {
            }

        }

        
        public DataSet GetElementInvariantProperty(int ApplicationID, int ElementID)
        {
            try
            {
                DataSet ds = new DataSet();
              

                this.cmd = new SqlCommand("dbo.usp_get_element_invariant_property");
                this.cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter par = new SqlParameter("@ApplicationID", SqlDbType.Int);
                par.Value = ApplicationID;
                this.cmd.Parameters.Add(par);
                SqlParameter par2 = new SqlParameter("@ElementID", SqlDbType.Int);
                par2.Value = ElementID;
                this.cmd.Parameters.Add(par2);

                cmd.Connection = sqlToolkit2Conn;
                SqlDataAdapter oDa = new SqlDataAdapter();
                oDa.SelectCommand = cmd;
                oDa.Fill(ds);


                return ds;
            }
            finally
            {
            }

        }

        public Boolean UpdateInvariant(int ElementID, string PropertyValue)
        {
            try
            {
                


                this.cmd = new SqlCommand("dbo.usp_update_invariant_value");
                this.cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter par = new SqlParameter("@ElementID", SqlDbType.Int);
                par.Value = ElementID;
                this.cmd.Parameters.Add(par);
                SqlParameter par2 = new SqlParameter("@PropertyValue", SqlDbType.NVarChar, 4000);
                par2.Value = PropertyValue;
                this.cmd.Parameters.Add(par2);
                if (ExecuteNonQuery() > 0)
                    return true;
                else
                    return false;

            }
            finally
            {
            }

        }
        
    }
}
